<template>
  <Modal v-model="modal" title="奖惩积分">
    <Form :label-width="100" ref="form" :model="form" :rules="rules">
      <FormItem label="奖惩积分：" prop="rewardIntegral">
        <InputNumber placeholder="最大50000，最小-50000" v-model="form.rewardIntegral" :precision="0" :min="-50000" :max="50000" class="wp100"></InputNumber>
      </FormItem>
      <FormItem label="原因：" prop="rewardRemark">
        <Input placeholder="最多50个字" v-model="form.rewardRemark" type="textarea" :rows="3" :maxlength="50"/>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button class="mr" size="large" type="text" @click="closeModal">取消</Button>
      <Button type="primary" size="large" :disabled="disabled" @click="onSave">确定</Button>
    </div>
  </Modal>
</template>
<script>
export default {
  data() {
    return {
      modal: false,
      disabled: false,
      storeContractId: null,
      form: {
        rewardIntegral: '',
        rewardRemark: ''
      },
      rules: {
        rewardIntegral: [{required: true, type: 'number', message: "奖惩积分在-50000到50000之间", trigger: "blur"}],
        rewardRemark: [{required: true, type: 'string', message: "请填写备注", trigger: "blur"}]
      }
    }
  },
  methods: {
    updataForm(id) {
      this.$refs.form.resetFields()
      this.form = {
        rewardIntegral: '',
        rewardRemark: ''
      }
      this.disabled =  false
      this.storeContractId = id
      this.openModal()
    },
    openModal() {
      this.modal = true;
    },
    closeModal() {
      this.modal = false
    },
    onSave() {
      if (this.disabled) return
      this.disabled = true
      this.$refs["form"].validate(valid => {
        let param = {
          storeContractId: this.storeContractId,
          rewardIntegral: this.form.rewardIntegral,
          rewardRemark: this.form.rewardRemark
        }
        if (valid) {
          this.$jsonPost(`/assets/store_reward/integral/reward`, param).then(data => {
            this.closeModal()
            this.$Notice.success({
              title: '操作成功'
            })
            this.disabled = false
          }).catch(() => {
            this.closeModal()
            this.disabled = false
          })
        } else {
          this.disabled = false
        }
      })
    }
  }
}
</script>
